package com.wzm.moviepic.fragment;

import net.tsz.afinal.FinalBitmap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.view.ActionBarView;
import com.wzm.bean.AdInfo;
import com.wzm.manager.AppConfig;
import com.wzm.moviepic.R;
import com.wzm.utils.Logger;
import com.wzm.utils.NetworkUtils;

public class AdIntroActivity extends Activity {
	private AdInfo adinfo = null;
	
	private RatingBar rBar = null;
	private TextView movie_rate = null;
	private ImageView bPic = null;
	private TextView mIntro = null;
	private TextView mName = null;
	private TextView mAuthor =null;
	private TextView mActor = null;
	private TextView mShowTime = null;
	
	
	private LinearLayout lly_down = null;
	private LinearLayout lly_read = null;
	private TextView tv_down,tv_read;
	
	private Button btn_mback = null;
	
	private FinalBitmap fb = null;
	
	private Context mContext = null;
	
    private int screen_width = 0;
    private int screen_height = 0;
    
    private LinearLayout rly_share = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ad);
		
		mContext = this;
		
		adinfo = (AdInfo)getIntent().getSerializableExtra("adinfo");
		if(adinfo==null)
		{
			Toast.makeText(this, "参数传递", Toast.LENGTH_SHORT).show();
			return;
		}
		
		
		fb = FinalBitmap.create(this,Environment.getExternalStorageDirectory().getPath()+AppConfig.IMGCACHE);
		//fb = FinalBitmap.create(mContext);
		fb.configLoadingImage(R.drawable.topwnd_default);
		fb.configLoadfailImage(R.drawable.topwnd_default);
		
		bPic = (ImageView)findViewById(R.id.movie_bpic);
		
		DisplayMetrics dm = new DisplayMetrics();  
        getWindowManager().getDefaultDisplay().getMetrics(dm);  
        screen_width = dm.widthPixels;  
		screen_height = dm.heightPixels;
		
		//ad_img
		//动态改变图片控件大小
		RelativeLayout.LayoutParams laParams=(RelativeLayout.LayoutParams)bPic.getLayoutParams();
		laParams.height=460*screen_width/640;
	    laParams.width=screen_width;
	    bPic.setLayoutParams(laParams);
		
		Logger.error(adinfo.getTop_img());
		fb.display(bPic, adinfo.getTop_img(),640,460);
//		//介绍
		mIntro = (TextView)findViewById(R.id.movie_intro);
		mIntro.setText(adinfo.getIntro());
//		
//		//名字
		mName = (TextView)findViewById(R.id.movie_name);
		mName.setText(adinfo.getName());

		
//		//ratebar
		rBar = (RatingBar)findViewById(R.id.ratingBar);
		rBar.setRating(Float.parseFloat(adinfo.getScore()));
//		
		movie_rate = (TextView)findViewById(R.id.movie_rate);
		movie_rate.setText(adinfo.getScore());
//		
//		类型
		mAuthor = (TextView)findViewById(R.id.movie_author);
		mAuthor.setText(adinfo.getApp_type());
//		联系
		mActor = (TextView)findViewById(R.id.movie_actor);
		mActor.setText(adinfo.getEmail());
//		制作
		mShowTime = (TextView)findViewById(R.id.movie_showtime);
		mShowTime.setText(adinfo.getCompany());
//		
		lly_down =(LinearLayout)findViewById(R.id.lly_down);
		tv_down = (TextView)findViewById(R.id.tv_down);
		tv_down.setText(adinfo.getOpen_url_title());
		lly_down.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				Uri uri = Uri.parse(adinfo.getOpen_url());  
				Intent it = new Intent(Intent.ACTION_VIEW, uri);  
				startActivity(it);
				it = null;
				
			}
		});
		
		lly_read = (LinearLayout)findViewById(R.id.lly_read);
		if(null!=adinfo.getAdvpic()&&adinfo.getAdvpic().size()>0)
		{
			lly_read.setVisibility(View.VISIBLE);
			lly_read.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub				
					Intent introintent = new Intent(mContext,AdViewPagerActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("adinfo", adinfo);
					introintent.putExtras(bundle);
					mContext.startActivity(introintent);
					introintent = null;
				}
			});
		}else{
			lly_read.setVisibility(View.GONE);
		}
		
		
		
		
		btn_mback = (Button)findViewById(R.id.btn_mback);
		btn_mback.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		
		//============配置ActionBar（可选）=======================================
        String share_content = adinfo.getIntro();
		UMSocialService controller = UMServiceFactory.getUMSocialService(adinfo.getName(),RequestType.SOCIAL);
		//设置分享文字
		controller.setShareContent(share_content);
		controller.setShareImage(new UMImage(mContext, adinfo.getImage()));
		
		if(NetworkUtils.getNetworkState(mContext)!=NetworkUtils.NETWORN_NONE)
        {
        	 //社会化分享
            rly_share = (LinearLayout)findViewById(R.id.share_bar);
            //创建ActionBar des参数是ActionBar的唯一标识，请确保不为空
            ActionBarView socializeActionBar = new ActionBarView(mContext,adinfo.getName());           
            LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            socializeActionBar.setLayoutParams(layoutParams);
            //添加ActionBar
            rly_share.addView(socializeActionBar);
        }		
		
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			finish();
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
	
}
